﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PDAMain.DAL;
using PDAMain.Entity;
using System.Runtime.InteropServices;

namespace PDAMain.Forms.ApplyToRepair
{
    public partial class ApplyToRepair : BaseForm
    {
        private bool tpFlag = true; //照相标记，控制是否能够保存
        private bool scanFlag = true;
        private string showFlag = string.Empty;
        public ApplyToRepair()
        {
            InitializeComponent();
            
        }
        //加载数据
        private void BindData()
        {
            recordeId.Text = Identity.dutyPerson;
            comApplyToRepair.Items.Clear();
            comApplyToRepair.Items.Insert(0, "--请选择--");
            comApplyToRepair.Items.Insert(1, "资产报修原因1");
            comApplyToRepair.Items.Insert(2, "资产报修原因2");
            comApplyToRepair.SelectedIndex = 0;

        }
        /// <summary>
        /// 报修
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItem1_Click(object sender, EventArgs e)
        {
            if (comApplyToRepair.SelectedIndex == 0)
            {
                MessageBox.Show("请选择报修原因！");
                return;
            }
            if (tpFlag)
            {
                MessageBox.Show("请对要报修的资产拍照！");
                return;
            }
            if (scanFlag)
            {
                MessageBox.Show("请扫描资产条码！"); 
                return;
            }
            string barCodeStr = barCode.Text.ToString() ;
            string recordeId = Identity.dutyPerson;
            string applyTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            string remarkStr = remark.Text.ToString().Trim();
            string reason = comApplyToRepair.SelectedItem.ToString().Trim();
            if (DataHelper.ApplyToRepairDao.insertApplyRepair(barCodeStr, recordeId, applyTime, remarkStr, reason,""))
            {
                MessageBox.Show("成功同步数据至远程数据库！");
                backToList();
                return;
            }
            else
            {
                DataHelper.ApplyToRepairDao.insertLacolTable(barCodeStr, recordeId, applyTime, remarkStr, reason, "1", "","");
                MessageBox.Show("同步数据失败，保存至本地数据库！"); 
                //往本地数据库插入该数据    to do
                backToList();
                return;
            }

        }
        /// <summary>
        /// 拍照
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void takePicture_Click(object sender, EventArgs e)
        {
            string folderName = "Repair";
            string strAssetNo = barCode.Text.ToString();
            if (strAssetNo == string.Empty)
            {
                MessageBox.Show("请先扫描资产编号");
                return;
            }
            TakePicture tp = new TakePicture();
            string strFileName = "";
            //维修前showFlag为空,维修中为1，维修后为2
            try
            {
                if (showFlag == string.Empty)
                {
                    strFileName = strAssetNo;
                    //tp.Save(strFileName);
                    pictureShow.Image = System.Drawing.Image.FromHbitmap(tp.Show(folderName, strFileName).GetHbitmap());
                    pictureShow.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                    showFlag = "1";
                    tpFlag = false;
                }
                
            }
            catch { }
            finally
            {
                tp.dispose();
            }
        }

        private void CustomerToRepair_Load(object sender, EventArgs e)
        {
            // 打开BarCode扫描设备
            if (!OpenBarCode())
            {
                /*
                int ErrCode = Marshal.GetLastWin32Error();
                MessageBox.Show("Fail to Open BarCode :(, ErrCode = " + ErrCode.ToString(), "Error");
                 */
                MessageBox.Show("打开扫描接口失败，请重登系统");
            }

            EnableBeeper(true);
            EnableVibrater(true);
        }

        // 重载 基类BaseForm的这个方法
        public override void OnBarCodeNotify(byte[] BarCodeData, int nLength)
        {
            EnableBarCode(false);

            string BarCode = Encoding.Default.GetString(BarCodeData, 0, nLength);
            barCode.Text = BarCode;

            //scanFlag置为false后才能保存本窗体数据
            scanFlag = false;
            EnableBarCode(true);
        }
        private void ApplyToRepair_Closed(object sender, EventArgs e)
        {
            // 关闭BarCode扫描设备
            CloseBarCode();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            FuntionView formShow = new FuntionView();
            formShow.Show();
            this.Close();
            CloseBarCode();
        }

        private void ApplyToRepair_Load(object sender, EventArgs e)
        {
            
            // 打开BarCode扫描设备
            if (!OpenBarCode())
            {
                /*
                int ErrCode = Marshal.GetLastWin32Error();
                MessageBox.Show("Fail to Open BarCode :(, ErrCode = " + ErrCode.ToString(), "Error");
                 */
                MessageBox.Show("打开扫描接口失败，请重登系统");
            }

            EnableBeeper(true);
            EnableVibrater(true);

            BindData();
        }
        private void backToList()
        {
            FuntionView formShow = new FuntionView();
            formShow.Show();
            this.Close();
        }
        

    }
}